<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RbacCommand
 *
 * @author Nhan Nguyen
 */
class RbacCommand extends CConsoleCommand{
    //put your code here
    private $_authManager;
    
    /*
     * 
     */
        /*
    public function getHelp()
    {
        return <<<EOD
        
        USAGE
            rbac
        
        DESCRIPTION
            This command generates an initial RBAC authorization hierarchy.
        
        EOD;
    }
        */
        
    public function run($args)
    {
        if(($this->_authManager = Yii::app()->authManager) === NULL){
            echo "Error: authManager must be configure to use this command";
            return;
        }
        
        // remove all operations, roles, child relationship and assignments
        $this->_authManager->clearAll();
        
        /*
         * Operations
         */
        // create operations for adminUser
        $this->_authManager->createOperation('AdminUser.*', 'full permissions of admin users');
        $this->_authManager->createOperation('AdminUser.admin', 'manage admin users');
        $this->_authManager->createOperation('AdminUser.index', 'list admin users');
        $this->_authManager->createOperation('AdminUser.create', 'create a new admin user');        
        $this->_authManager->createOperation('AdminUser.update', 'update an admin user information');
        $this->_authManager->createOperation('AdminUser.delete', 'remove an admin user');
        
        // operations for locale
        $this->_authManager->createOperation('Locale.*', 'full permissions of locales');
        $this->_authManager->createOperation('Locale.admin', 'manage locales');
        $this->_authManager->createOperation('Locale.index', 'list locales');
        $this->_authManager->createOperation('Locale.create', 'create a new locale');
        $this->_authManager->createOperation('Locale.update', 'update an locale information');
        $this->_authManager->createOperation('Locale.delete', 'remove a locale');
        
        // operations for upload
        $this->_authManager->createOperation('Upload.*', 'full permissions of uploads');
        $this->_authManager->createOperation('Upload.admin', 'manage uploads');
        $this->_authManager->createOperation('Upload.index', 'list uploads');
        $this->_authManager->createOperation('Upload.create', 'create a new upload');
        $this->_authManager->createOperation('Upload.update', 'update an upload information');
        $this->_authManager->createOperation('Upload.delete', 'remove a upload');
        
        // operations for category
        $this->_authManager->createOperation('Category.*', 'full permissions of category');
        $this->_authManager->createOperation('Category.admin', 'manage category');
        $this->_authManager->createOperation('Category.index', 'list category');
        $this->_authManager->createOperation('Category.create', 'create a new category');
        $this->_authManager->createOperation('Category.update', 'update an category information');
        $this->_authManager->createOperation('Category.delete', 'remove a category');
        
        // operations for series
        $this->_authManager->createOperation('Series.*', 'full permissions of series');
        $this->_authManager->createOperation('Series.admin', 'manage series');
        $this->_authManager->createOperation('Series.index', 'list series');
        $this->_authManager->createOperation('Series.create', 'create a new series');
        $this->_authManager->createOperation('Series.update', 'update an series information');
        $this->_authManager->createOperation('Series.delete', 'remove a series');
        
        // operations for article
        $this->_authManager->createOperation('Article.*', 'full permissions of article');
        $this->_authManager->createOperation('Article.admin', 'manage article');
        $this->_authManager->createOperation('Article.index', 'list article');
        $this->_authManager->createOperation('Article.create', 'create a new article');
        $this->_authManager->createOperation('Article.update', 'update an article information');
        $this->_authManager->createOperation('Article.delete', 'remove a article');
        
        /*
         * Roles
         */
        // guest
        $bizRule = 'return !Yii::app()->user->isGuest;';
        $role = $this->_authManager->createRole('Guest', 'Guest user', $bizRule);
        $role->addChild('Article.index');
        
        // update own post task
        $bizRule = 'return Yii::app()->user->id = $params[\'post\']->id';
        $task = $this->_authManager->createTask('updateOwnPost', 'Update the author articles', $bizRule);
        $task->addChild('Article.*');
        
        // register user
        $role = $this->_authManager->createRole('Registered User', 'Registered user');
        $role->addChild('updateOwnPost');
        
        // moderator
        $role = $this->_authManager->createRole('Moderator');
        $role->addChild('Article.*');
        $role->addChild('Series.*');
        $role->addChild('Upload.*');
        
        // admin
        $role = $this->_authManager->createRole('Administrator');
        $role->addChild('Moderator');
        $role->addChild('Category.*');
        $role->addChild('Locale.*');
        $role->addChild('AdminUser.*');
        
        /*
         * Users
         */
        // User ID 1 will be an administrator
        $this->_authManager->assign('Administrator', 1);
        $this->_authManager->assign('Moderator', 2);
        $this->_authManager->assign('Registered User', 3);
    }
}
